package com.skivingcloud.report.datas.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.report.datas.entity.OriginalDataDto;
import com.skivingcloud.report.datas.entity.ReportDataset;
import com.skivingcloud.report.datas.entity.ReportDatasetParam;
import com.skivingcloud.report.datas.service.IReportDatasetParamService;
import com.skivingcloud.report.datas.service.IReportDatasetService;

/**
 * <p>
 * 数据集管理 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/datas/reportDataset")
public class ReportDatasetController {

	private static Logger log = LoggerFactory.getLogger(ReportDatasetController.class);

	@Autowired
	private IReportDatasetService reportDatasetService;
	@Autowired
	private IReportDatasetParamService reportDatasetParamService;

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = reportDatasetService.queryPage(params);
		return R.ok().put("page", pages);
	}
	
	/**
     * 测试 数据转换是否正确
     * @param dataSet
     * @return
	 * @throws Exception
     */
    @PostMapping("/testTransform")
    public R testTransform(@RequestBody ReportDataset dataSet) throws Exception {
		OriginalDataDto dto =  reportDatasetService.testTransform(dataSet);
        return R.ok().put("data", dto);
    }
	
	/**
	 * 测试 数据转换是否正确
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/testTransformSetBySetId/{id}")
	public R testTransformSetBySetId(@PathVariable("id") String id) throws Exception {
		OriginalDataDto dto =  reportDatasetService.testTransformBySetId(id);
        return R.ok().put("data", dto);
	}
	/**
	 * 新增
	 * 
	 * @param dataSet
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "save", description = "新增数据集数据")
	@PostMapping("/save")
	public R save(@RequestBody ReportDataset dataSet) {
		try {
			reportDatasetService.add(dataSet, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据集数据失败!", e);
			return R.error("保存数据集数据失败!");
		}
	}

	/**
	 * 修改
	 * 
	 * @param dataSet
	 * @return
	 */
	@UserOperateLog(module = "报表管理", methods = "update", description = "修改数据集数据")
	@PostMapping("/update")
	public R update(@RequestBody ReportDataset dataSet) {
		try {
			reportDatasetService.update(dataSet, UserUtil.getUser());
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据集数据失败!", e);
			return R.error("保存数据集数据失败!");
		}
	}

	/**
	 * 根据主键查询
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		ReportDataset reportDataset = reportDatasetService.selectById(id);
		return R.ok().put("data", reportDataset);
	}

	/**
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/verificationSet")
	public R verificationSet(@RequestBody ReportDatasetParam param) {
		try {
			Object obj = reportDatasetParamService.verification(param);
			return R.ok().put("data", obj);
		} catch (Exception e) {
			log.error("高级规则校验失败!", e);
			return R.error("高级规则校验失败!");
		}
	}

	@GetMapping("/queryAllDataSet")
	public R queryAllDataSet() throws Exception {
		List<ReportDataset> dataSet =  reportDatasetService.queryAllDataSet();
        return R.ok().put("data", dataSet);
	}

	@GetMapping("/detailBysetId")
	public R detailBysetId(@PathVariable("id") String id) throws Exception {
		ReportDataset reportDataset =  reportDatasetService.detailBysetId(id);
        return R.ok().put("data", reportDataset);
	}
}
